package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.bean.Student;
import com.naughty.userlogin02.bean.Teacher;
import com.naughty.userlogin02.dao.TeacherDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
@RestController
public class TeacherController {

    @Autowired
    TeacherDao teacherDao;

    @CrossOrigin
    @RequestMapping("/allTeacher")
    public String getTeacherList(QueryInfo queryInfo) {

        System.out.println("hhhhhhhhhhhh");
        System.out.println(queryInfo);
        int numbers = teacherDao.getTeacherCounts("%" + queryInfo.getQuery() + "%");// 获取数据总数
        System.out.println("aaaaaa");
        System.out.println(numbers);

        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Teacher> users = teacherDao.getAllTeacher("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers", numbers);
        res.put("data", users);
        System.out.println("总条数：" + numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println("pppppp");
        System.out.println(users_json);
        return users_json;
    }

    @RequestMapping("/addTeacher")
    public String addTeacher(@RequestBody Teacher teacher) {
        System.out.println("bbbbbbbbbbbb");
        System.out.println(teacher);
//        teacher.setRole("普通用户");
//        teacher.setSpwd("12345");
        int i = teacherDao.addTeacher(teacher);
        String str = i > 0 ? "success" : "error";
        System.out.println("bbaaaa");
        System.out.println(i);
        return str;//也可以通过这里返回报错
    }

    @RequestMapping("/editTeacher")
    public String editTeacher(@RequestBody Teacher teacher) {
        System.out.println(teacher);
        int i = teacherDao.editTeacher(teacher);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/delete_tid")
    public String deleteTeacher(@RequestBody Teacher teacher) {
        System.out.println(teacher.getTid());
        int i = teacherDao.deleteTeacher(teacher.getTid());
        String str = i > 0 ? "success" : "error";
        return str;
    }

}

